package com.dimeng.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dimeng.R;
import com.dimeng.entity.Info;
import com.dimeng.entity.InfoViewCache;
import com.dimeng.utils.AsyncImageLoader;
import com.dimeng.utils.AsyncImageLoader.ImageCallback;

/**
 * @author write by ccn<br>
 *         <b>helloandroid100@gmail.com</b>
 * @version 0.1;2012-5-18 下午5:30:44<br>
 *          descrition:资讯列表的数据适配器
 */
public class InfoAdapter extends ArrayAdapter<Info> {
	AsyncImageLoader asyncImageLoader = AsyncImageLoader.getInstance();
	private ListView listView;
	public InfoAdapter(Context context,List<Info> infos, ListView listView) {
		super(context, 0, infos);
		this.listView = listView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();
		// Inflate the views from XML
		View infoRowView = convertView;
		Info info = getItem(position);
		InfoViewCache infoViewCache;
		if (infoRowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			infoRowView = inflater.inflate(R.layout.info_item, null);
			infoViewCache = new InfoViewCache(infoRowView);
			infoRowView.setTag(infoViewCache);
		} else {
			infoViewCache = (InfoViewCache) infoRowView.getTag();
		}
		// Set the text on the TextView
		TextView titleTextView = infoViewCache.getTitleView();
		String infoTitle = info.getTitle();
		int maxInfoTitleLen = 37;
		infoTitle = infoTitle.length()>maxInfoTitleLen?infoTitle.subSequence(0, maxInfoTitleLen)+"...":infoTitle;
		titleTextView.setText(infoTitle);
		TextView createTimeTextView = infoViewCache.getCreateTimeView();
		createTimeTextView.setText(info.getCreateTime());
		TextView prefixContentView = infoViewCache.getPrefixContentView();
		String prefixContent = info.getPrefixContent();
		if(null != prefixContent){
			int maxPrefixContentLen = 36;
			prefixContent = prefixContent.length() > maxPrefixContentLen?prefixContent.substring(0, maxPrefixContentLen)+"...":prefixContent;
		}
		prefixContentView.setText(prefixContent);
		
		// Load the image and set it on the ImageView
		String imageUrl = info.getImageUrl();
		ImageView imageView = infoViewCache.getImageView();
		imageView.setTag(imageUrl);
		if(null != imageUrl && !"".equals(imageUrl.trim())){
			Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
				public void imageLoaded(Drawable imageDrawable, String imageUrl) {
					ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
					if (imageViewByTag != null) {
						imageViewByTag.setBackgroundDrawable(imageDrawable);
					}
				}
			});
			imageView.setBackgroundDrawable(cachedImage);
		} else {
			imageView.setBackgroundDrawable(null);
		}
		
		return infoRowView;
	}
}
